package scau.gsmg.misp.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Service;
import scau.gsmg.misp.dao.IMenuDao;
import scau.gsmg.misp.service.IMenuService;
import scau.gsmg.misp.vo.Permission;

@Service
public class MenuService implements IMenuService {

	private IMenuDao menuDao;

	@Resource
	public void setMenuDao(IMenuDao menuDao) {
		this.menuDao = menuDao;
	}

	private List<Permission> searchMenu(int loginID){
		return 	menuDao.searchMenu(loginID);
	}
	private List<Permission> permissions;
	/**
	 * 获取菜单树
	 */
	@Override
	public List showMenu(int loginID) {

//		List<Permission> permissions = menuDao.searchMenu(loginID);
		permissions = searchMenu(loginID);
		List list = new ArrayList();
		Map<String, Object> map = null;
		for (Permission p : permissions) {// 遍历树节点为父节点的点
			if (p.getSort() == null || "".equals(p.getSort())) {
				map = new HashMap<String, Object>();
				map.put("id", p.getId());
				map.put("text", p.getName());
				if ("1".equals(p.getState())) {
					List kk = new ArrayList();
					kk = nodeMenu(p, loginID);
					map.put("children", kk);
				} else {
					if (p.getUrl() != null) {
						Map<String, Object> mapUrl = new HashMap<String, Object>();
						mapUrl.put("url", p.getUrl());
						map.put("attributes", mapUrl);
					}
				}
				list.add(map);
			}
		}
		return list;
	}

	/**
	 * @Description:递归处理树节点
	 */
	private List nodeMenu(Permission per, int loginID) {

//		List<Permission> permissions = menuDao.searchMenu(loginID);
	//	List<Permission> permissions = searchMenu(loginID);
		List listNode = new ArrayList();
		Map<String, Object> map = null;
		for (Permission p : permissions) {
			if (p.getSort() != null && p.getSort().equals(per.getId() + "")) {
				map = new HashMap<String, Object>();
				map.put("id", p.getId());
				map.put("text", p.getName());
				if ("1".equals(p.getState())) {
					List ll = nodeMenu(p, loginID);
					map.put("children", ll);

				} else {
					if (p.getUrl() != null) {
						Map<String, Object> mapUrl = new HashMap<String, Object>();
						mapUrl.put("url", p.getUrl());
						map.put("attributes", mapUrl);
					}

				}
				listNode.add(map);
			}
		}
		return listNode;
	}

	@Override
	public List distributePermission(int role) {
		// TODO Auto-generated method stub
		return menuDao.distributePermission(role);
	}

	/**
	 * 对角色权限的处理
	 */
	@Override
	public boolean editPermissions(int role, String rowObj) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(rowObj);// 把从前台得到的json字符串转换成json数组
		// 将每一条商品记录保存到数据库
		boolean delete = menuDao.deleteAll(role);
		for (int i = 0; i < jsonArray.size(); i++) {//对json分解处理
			int id = jsonArray.getJSONObject(i).getInt("id");
			JSONArray jsonArrayChildren = jsonArray.getJSONObject(i).getJSONArray("children");
			int idChildren = 0;
			for (int z = 0; z < jsonArrayChildren.size(); z++) {
				boolean editPermission = false;
				boolean editRolePermission = false;
				boolean editOperatePermission = false;
				idChildren = jsonArrayChildren.getJSONObject(z).getInt("id");
				for (int j = 1; j < 5; j++) {
					String pid = jsonArrayChildren.getJSONObject(z).getString(
							j + "");
					if ("P".equals(pid)) {
						editPermission = menuDao.editPermission(role,
								idChildren, j);
					}
				}
				String browse = jsonArrayChildren.getJSONObject(z).getString(
						"browse");
				String operate = jsonArrayChildren.getJSONObject(z).getString(
						"5");
				System.out.println("String operate" + operate);
				if ("P".equals(browse)) {
					editRolePermission = menuDao.editRolePermission(role,
							idChildren);
				}
				if ("P".equals(operate)) {

					editOperatePermission = menuDao.editRolePermission(role,
							idChildren);
				}
			}
		}
		if (delete) {
			return true;
		}
		return false;
	}
	@Override
	public List showPermission(int id) {
		return menuDao.showPermission(id);
	}

}
